/*
 * Copyright 2006 Marc Wick, geonames.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package ca.cbc.gisdigester.eveparser;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Locale;

import org.jdom.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sun.syndication.feed.module.Module;
import com.sun.syndication.io.ModuleParser;

public class EveRSSParser implements ModuleParser {
	private static Logger logger = LoggerFactory.getLogger(EveRSSParser.class);
	private DateFormat rssDateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm zzz", Locale.ENGLISH);
	private DateFormat rssDateFormatWithSec = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.ENGLISH);
	/*
	 * (non-Javadoc)
	 * 
	 * @see com.sun.syndication.io.ModuleParser#getNamespaceUri()
	 */
	public String getNamespaceUri() {
		return EveRSSModule.EVERSS_URI;
	}

	private long praseTime(String date) {
		long time = 0l;
		try {
			time = rssDateFormat.parse(date).getTime();
		} catch (ParseException e) {
			try {
				time = rssDateFormatWithSec.parse(date).getTime();
			} catch (ParseException e1) {
				logger.warn("Parse rss date formate error", e);
			}
		}
		return time;
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see com.sun.syndication.io.ModuleParser#parse(org.jdom.Element)
	 */
	public Module parse(Element element) {
		EveRSSModuleImpl eventModuleImpl = new EveRSSModuleImpl();
		
		Element address = element.getChild(EveRSSModule.ELEMENT_NAME_ADDRESS, EveRSSModule.EVERSS_NS);
		if (address != null) {
			eventModuleImpl.setAddress(address.getText());
		}
		
		Element audience = element.getChild(EveRSSModule.ELEMENT_NAME_AUDIENCE, EveRSSModule.EVERSS_NS);
		if (audience != null) {
			eventModuleImpl.setAudience(audience.getText());
		}
		
		Element category = element.getChild(EveRSSModule.ELEMENT_NAME_CATEGORY, EveRSSModule.EVERSS_NS);
		if (category != null) {
			eventModuleImpl.setCategory(category.getText());
		}
		
		Element city = element.getChild(EveRSSModule.ELEMENT_NAME_CITY, EveRSSModule.EVERSS_NS);
		if (city != null) {
			eventModuleImpl.setCity(city.getText());
		}
		
		Element company = element.getChild(EveRSSModule.ELEMENT_NAME_COMPANY, EveRSSModule.EVERSS_NS);
		if (company != null) {
			eventModuleImpl.setCompany(company.getText());
		}
		
		Element coordinator = element.getChild(EveRSSModule.ELEMENT_NAME_COORDINATOR, EveRSSModule.EVERSS_NS);
		if (coordinator != null) {
			eventModuleImpl.setCoordinator(coordinator.getText());
		}
		
		Element email = element.getChild(EveRSSModule.ELEMENT_NAME_EMAIL, EveRSSModule.EVERSS_NS);
		if (email != null) {
			eventModuleImpl.setEmail(email.getText());
		}
		
		Element end = element.getChild(EveRSSModule.ELEMENT_NAME_END, EveRSSModule.EVERSS_NS);
		if (end != null) {
			long endTime = praseTime(end.getText());
			if(endTime != 0l) {
				eventModuleImpl.setEnd(endTime);
			}
		}
		
		Element start = element.getChild(EveRSSModule.ELEMENT_NAME_START, EveRSSModule.EVERSS_NS);
		if (start != null) {
			long startTime = praseTime(start.getText());
			if(startTime != 0l) {
				eventModuleImpl.setStart(startTime);
			}
		}
		
		Element free = element.getChild(EveRSSModule.ELEMENT_NAME_FREE, EveRSSModule.EVERSS_NS);
		if (free != null) {
			eventModuleImpl.setFree(free.getText());
		}
		Element link = element.getChild(EveRSSModule.ELEMENT_NAME_LINK, EveRSSModule.EVERSS_NS);
		if (link != null) {
			eventModuleImpl.setLink(link.getText());
		}
		Element name = element.getChild(EveRSSModule.ELEMENT_NAME_NAME, EveRSSModule.EVERSS_NS);
		if (name != null) {
			eventModuleImpl.setName(name.getText());
		}
		Element phone = element.getChild(EveRSSModule.ELEMENT_NAME_PHONE, EveRSSModule.EVERSS_NS);
		if (phone != null) {
			eventModuleImpl.setPhone(phone.getText());
		}
		Element price = element.getChild(EveRSSModule.ELEMENT_NAME_PRICE, EveRSSModule.EVERSS_NS);
		if (price != null) {
			eventModuleImpl.setPrice(price.getText());
		}
		
		Element province = element.getChild(EveRSSModule.ELEMENT_NAME_PROVINCE, EveRSSModule.EVERSS_NS);
		if (province != null) {
			eventModuleImpl.setProvince(province.getText());
		}
		
		Element postalcode = element.getChild(EveRSSModule.ELEMENT_NAME_POSTALCODE, EveRSSModule.EVERSS_NS);
		if (postalcode != null) {
			eventModuleImpl.setPostalcode(postalcode.getText());
		}
		
		Element repeats = element.getChild(EveRSSModule.ELEMENT_NAME_REPEATS, EveRSSModule.EVERSS_NS);
		if (repeats != null) {
			eventModuleImpl.setRepeats(repeats.getText());
		}
		
		Element lat = element.getChild(EveRSSModule.ELEMENT_NAME_LATITUDE, EveRSSModule.EVERSS_NS);
		if (lat != null) {
			eventModuleImpl.setLatitude(Double.parseDouble(lat.getText()));
		}
		
		Element lng = element.getChild(EveRSSModule.ELEMENT_NAME_LONGITUDE, EveRSSModule.EVERSS_NS);
		if (lng != null) {
			eventModuleImpl.setLongitude(Double.parseDouble(lng.getText()));
		}
		return eventModuleImpl;
	}
}
